﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using System.Collections;
namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 业务逻辑类Base_Permit_Define 的摘要说明。
    /// </summary>
    public class Base_Permit_Define
    {

        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        public Base_Permit_Define()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid PermitID)
        {
            return Chenxin.Lib.Hr.DAL.Base_Permit_Define.Exists(PermitID);
        }
        /// <summary>
        /// 是否存在权限编号
        /// </summary>
        public bool ExistsCode(string PermitCode)
        {
            return Chenxin.Lib.Hr.DAL.Base_Permit_Define.ExistsCode(PermitCode);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.Base_Permit_Define model)
        {
            Chenxin.Lib.Hr.DAL.Base_Permit_Define.Add(model);
            //查找Cache的权限键
            string strKeyAll10 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "10");
            string strKeyAll20 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "20");
            string strKeyAll30 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "30");
            //删除权限列表
            cs.Delete(strKeyAll10);
            cs.Delete(strKeyAll20);
            cs.Delete(strKeyAll30);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.Base_Permit_Define model)
        {
            Chenxin.Lib.Hr.DAL.Base_Permit_Define.Update(model);
            //Cache的权限列表键
            string strKeyAll10 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "10");
            string strKeyAll20 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "20");
            string strKeyAll30 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "30");
            //单个权限键
            string strKeySingle = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_SINGLEPERMIT, model.PermitID.ToString());
            //分类对应权限列表
            string strKeyGroupPermits = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_GROUPPERMITS, model.PermitGroup);

            //删除权限列表
            cs.Delete(strKeyAll10);
            cs.Delete(strKeyAll20);
            cs.Delete(strKeyAll30);
            //删除单个权限
            cs.Delete(strKeySingle);
            //删除分类对应权限列表
            cs.Delete(strKeyGroupPermits);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid PermitID)
        {

            Chenxin.Lib.Hr.DAL.Base_Permit_Define.Delete(PermitID);
            //Cache的权限列表键
            string strKeyAll10 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "10");
            string strKeyAll20 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "20");
            string strKeyAll30 = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, "30");
            //单个权限键
            string strKeySingle = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_SINGLEPERMIT, PermitID.ToString());
            
            //删除权限列表
            cs.Delete(strKeyAll10);
            cs.Delete(strKeyAll20);
            cs.Delete(strKeyAll30);
            //删除单个权限
            cs.Delete(strKeySingle);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.Base_Permit_Define GetModel(Guid PermitID)
        {
            Chenxin.Lib.Hr.Entity.Base_Permit_Define entBasePermitDefine;
            //查找Cache的用户权限键
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_SINGLEPERMIT, PermitID.ToString());
            //从Cache中取得单个权限
            entBasePermitDefine = cs.GetObject<Chenxin.Lib.Hr.Entity.Base_Permit_Define>(strCacheKey);
            //未命中
            if (!cs.Hit)
            {
                //从数据库中搜索
                entBasePermitDefine = Chenxin.Lib.Hr.DAL.Base_Permit_Define.GetModel(PermitID);
                //加入到Cache中
                cs.Set(strCacheKey, entBasePermitDefine);
            }
            return entBasePermitDefine;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Hr.DAL.Base_Permit_Define.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Hr.DAL.Base_Permit_Define.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> modelList = new List<Chenxin.Lib.Hr.Entity.Base_Permit_Define>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.Base_Permit_Define model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.Base_Permit_Define();
                    if (dt.Rows[n]["PermitID"].ToString() != "")
                    {
                        model.PermitID = new Guid(dt.Rows[n]["PermitID"].ToString());
                    }
                    model.PermitCode = dt.Rows[n]["PermitCode"].ToString();
                    model.PermitName = dt.Rows[n]["PermitName"].ToString();
                    model.PermitGroup = dt.Rows[n]["PermitGroup"].ToString();
                    if (dt.Rows[n]["ShowLevel"].ToString() != "")
                    {
                        model.ShowLevel = int.Parse(dt.Rows[n]["ShowLevel"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        public ArrayList GetModelListDividedByType()
        {
            return GetModelListDividedByType(10);
        }
        //*******************************************
        //功能概要       ：按类型进行从新组织数据
        //参数説明       ：无
        //返回值説明     ：按类型分类的权限列表
        //功能詳細       ：按类型进行从新组织数据
        //作成者         ：he.jifeng 
        //作成日         ：2010-02-26 
        //*******************************************
        public ArrayList GetModelListDividedByType(int ShowLevel)
        {
            string strWhere;

            ArrayList lsPermitsDividedByType;
            //查找Cache的用户权限键
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_PERMITS_LEVEL, ShowLevel);

            //从Cache中查找权限列表
            lsPermitsDividedByType = (ArrayList)cs.GetObject(strCacheKey);

            //Cache未命中
            if (!cs.Hit)
            {
                strWhere = string.Format(" ShowLevel >={0}", ShowLevel);
                //得到权限全表
                DataSet dsPermits = GetList(strWhere);
                //分组后的列表
                lsPermitsDividedByType = new ArrayList();

                int rowsCount = dsPermits.Tables[0].Rows.Count;
                //有数据的场合
                if (rowsCount > 0)
                {
                    //前一个类别
                    string prePermitType = "";
                    //临时类别
                    string tempPermitType;
                    //子权限表
                    List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> lsCldPermits = new List<Chenxin.Lib.Hr.Entity.Base_Permit_Define>();

                    Chenxin.Lib.Hr.Entity.Base_Permit_Define model;

                    //循环全表
                    for (int idxRow = 0; idxRow < rowsCount; idxRow++)
                    {
                        tempPermitType = dsPermits.Tables[0].Rows[idxRow]["PermitGroup"].ToString();
                        //第一次循环时赋值
                        if (idxRow == 0)
                        {
                            //前一个类别赋值
                            prePermitType = tempPermitType;
                            //新建子类别列表对象
                            lsCldPermits = new List<Chenxin.Lib.Hr.Entity.Base_Permit_Define>();

                            model = new Chenxin.Lib.Hr.Entity.Base_Permit_Define();
                            model.PermitID = new Guid(dsPermits.Tables[0].Rows[idxRow]["PermitID"].ToString());
                            model.PermitCode = dsPermits.Tables[0].Rows[idxRow]["PermitCode"].ToString();
                            model.PermitName = dsPermits.Tables[0].Rows[idxRow]["PermitName"].ToString();
                            model.PermitGroup = dsPermits.Tables[0].Rows[idxRow]["PermitGroup"].ToString();
                            //添加到子类别列表中
                            lsCldPermits.Add(model);
                        }
                        else
                        {
                            //当前类别和前一类别相同的情况下
                            if (prePermitType == tempPermitType)
                            {
                                model = new Chenxin.Lib.Hr.Entity.Base_Permit_Define();
                                model.PermitID = new Guid(dsPermits.Tables[0].Rows[idxRow]["PermitID"].ToString());
                                model.PermitCode = dsPermits.Tables[0].Rows[idxRow]["PermitCode"].ToString();
                                model.PermitName = dsPermits.Tables[0].Rows[idxRow]["PermitName"].ToString();
                                model.PermitGroup = dsPermits.Tables[0].Rows[idxRow]["PermitGroup"].ToString();
                                //添加到子类别列表中
                                lsCldPermits.Add(model);
                            }
                            else
                            {
                                lsPermitsDividedByType.Add(lsCldPermits);
                                //前一个类别赋值
                                prePermitType = tempPermitType;
                                //新建子类别列表对象
                                lsCldPermits = new List<Chenxin.Lib.Hr.Entity.Base_Permit_Define>();

                                model = new Chenxin.Lib.Hr.Entity.Base_Permit_Define();
                                model.PermitID = new Guid(dsPermits.Tables[0].Rows[idxRow]["PermitID"].ToString());
                                model.PermitCode = dsPermits.Tables[0].Rows[idxRow]["PermitCode"].ToString();
                                model.PermitName = dsPermits.Tables[0].Rows[idxRow]["PermitName"].ToString();
                                model.PermitGroup = dsPermits.Tables[0].Rows[idxRow]["PermitGroup"].ToString();
                                //添加到子类别列表中
                                lsCldPermits.Add(model);
                            }
                        }

                        //最后一个数据的情况下
                        if (idxRow == rowsCount - 1)
                        {
                            lsPermitsDividedByType.Add(lsCldPermits);
                        }

                    }
                    //加入到Cache中
                    cs.Set(strCacheKey, lsPermitsDividedByType);
                }
            }
            
            return lsPermitsDividedByType;
        }

        //*******************************************
        //功能概要       ：根据类型取得权限列表
        //参数説明       ：权限类型名
        //返回值説明     ：权限列表
        //功能詳細       ：根据类型取得权限列表
        //作成者         ：he.jifeng 
        //作成日         ：2010-02-26 
        //*******************************************
        public List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> GetModelListByGroup(string strPermitGroup)
        {
            List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> lsBasePermitDefine;

            //某一权限类型对应权限列表的键
            string strKeyGroupPermits = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_GROUPPERMITS, strPermitGroup);
            //从Cache中查找
            lsBasePermitDefine = cs.GetList<Chenxin.Lib.Hr.Entity.Base_Permit_Define>(strPermitGroup);
            //未命中
            if (!cs.Hit)
            {
                //where文
                string strWhere;

                strWhere = "PermitGroup = '" + strPermitGroup +"'";
                //从数据库中查找
                DataSet ds = Chenxin.Lib.Hr.DAL.Base_Permit_Define.GetList(strWhere);
                lsBasePermitDefine = DataTableToList(ds.Tables[0]);
                //添加到Cache中
                cs.Set(strKeyGroupPermits,lsBasePermitDefine);
            }

            return lsBasePermitDefine;
        }
        #endregion  成员方法
    }
}

